//
//  MBLoadingView.m
//  ltsport
//
//  Created by iOS on 2020/6/16.
//  Copyright © 2020 MengBao. All rights reserved.
//

#import "MBLoadingView.h"
#import "UIImage+GIF.h"
@interface MBLoadingView ()
//@property (nonatomic, strong) UIImageView *backgroundImageView;
//@property (nonatomic, strong) UIImageView *loadingImageView;

@property (weak, nonatomic) UIActivityIndicatorView  *indicatorView;
@property (assign, nonatomic) BOOL showLoading;
@end

@implementation MBLoadingView
/* 创建单列 */
+(instancetype)shareLoadingView
{
    static MBLoadingView *loadingView = nil;
    //给单例加了一个线程锁
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        loadingView = [[MBLoadingView alloc] initWithFrame:CGRectZero];

    });
    return loadingView;
}

- (UIActivityIndicatorView *)indicatorView {
    if (!_indicatorView) {
        UIActivityIndicatorView *obj = [UIActivityIndicatorView new];
        [self addSubview:_indicatorView = obj];
        obj.activityIndicatorViewStyle = UIActivityIndicatorViewStyleMedium;
        // 改变圈圈大小
        obj.transform = CGAffineTransformMakeScale(2, 2);
    }
    return _indicatorView;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.indicatorView.center = self.center;
}

#pragma mark - Public Method
- (void)showLoadingView {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新UI的代码放到主线程执行
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.right.left.mas_equalTo(0);
            make.top.mas_equalTo(0);
        }];
        
        self.showLoading = YES;
    });
    
}

-(void)hideLoadingView {
    self.showLoading = NO;
    [self removeFromSuperview];
}

//-(void)changeFrame:(CGRect)frame
//{
//    self.frame = frame;
//    self.backgroundImageView.frame = self.bounds;
//    self.loadingImageView.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
//}

- (void)setShowLoading:(BOOL)showLoading{
    _showLoading = showLoading;
    self.hidden  = !_showLoading;
    if (_showLoading) {
        [self.indicatorView startAnimating];
    }else{
        [self.indicatorView stopAnimating];
    }
}

@end
